/**
 * @author Helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.ClassificationsDao) SALE.dao.local.ClassificationsDao = function(){
	var classificationsDoc;
}

SALE.dao.local.ClassificationsDao.prototype = {	
		
		initDoc: function() {
			if(!this.classificationsDoc) {
				this.classificationsDoc = LocalXMLFileUtil.getClassificationDoc();
			}
			return this.classificationsDoc;
		},
		
		saveDoc : function() {
			LocalXMLFileUtil.saveXML(this.classificationsDoc,LocalXMLFileUtil.getClassificationFile());
		},
		
		insertClassificationInfo : function(classificationJson) {
			var root = this.classificationsDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			classificationJson["id"] = id;
			root.setAttribute("maxid",id);
			var classificationNode = this.classificationsDoc.createElement("classification");
			
			this.updateClassification(classificationJson, classificationNode);			
			root.appendChild(classificationNode);
			
			this.saveDoc();
		},
		
		modifyClassificationInfo : function(classificationJson) {
			var classificationNode = this.searchClassificationByID(classificationJson.id);
			
			this.updateClassification(classificationJson, classificationNode);
			
			this.saveDoc();
		},
		
		deleteClassificationInfo : function(id) {
			var classificationNode = this.searchClassificationByID(id);
			var root = this.classificationsDoc.documentElement;
			if(classificationNode != null) {
				root.removeChild(classificationNode);
			}			
		},
				
		updateClassification : function(classificationJosn, classificationNode) {
			
			for(var attri in classificationJosn) {	
				
				if(attri.indexOf("old") != 0 && classificationJosn[attri] && classificationJosn[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, classificationNode);
					if(attriNodes != null && attriNodes.length > 0) {
						classificationNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.classificationsDoc.createElement(attri);
					var contextNode = this.classificationsDoc.createCDATASection(classificationJosn[attri]);
					attriNode.appendChild(contextNode);
					classificationNode.appendChild(attriNode);
				}
			}
		},
		
		searchClassificationByID : function(id) {
			var root = this.classificationsDoc.documentElement;
			var objArray = Ext.query("/classification:has(id:nodeValue(" + id+ "))", root);
			var result;
			
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchClassificationJsonByID : function(id) {
			
			var resultNode = this.searchClassificationByID(id);
			
			var result = {};
			
			if (resultNode != null) {
				var children = resultNode.childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			
			return result;
		},
		
		buildClassificationsJson: function() {
			var root = this.initDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/classification", root);
				var classificationsJson = {classifications: []};
				
				Ext.each(objArray, function(classification) {
					var children = classification.childNodes;
					var classificationJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!classificationJson[child.nodeName] && child.childNodes[0] != null) {
								classificationJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					classificationsJson.classifications.push(classificationJson);
				});
				
				return classificationsJson;
			}
		}	
}
